package com.Util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.Classes.Order;
import com.Main.faislamovers.R;

public class OrdersAdapter extends ArrayAdapter<Order> {

	public Date date_time;
	Context context;
	int layoutResourceId;
	ArrayList<Order> listArr;

	public OrdersAdapter(Context context, int layoutId,
			ArrayList<Order> myOrderLIst) {
		super(context, layoutId, myOrderLIst);
		this.layoutResourceId = layoutId;
		this.context = context;
		listArr = myOrderLIst;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		OrderHolder holder = null;
	
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.listview_item_row, null);

			holder = new OrderHolder();
			holder.txtDestination = (TextView) convertView
					.findViewById(R.id.txtDestination);
			holder.txtSource = (TextView) convertView
					.findViewById(R.id.txtSource);
			holder.txtReachTime = (TextView) convertView
					.findViewById(R.id.txtEndTime);
			holder.txtLeaveTime = (TextView) convertView
					.findViewById(R.id.txtStartTime);
			holder.txtTime = (TextView) convertView.findViewById(R.id.txtTime);
			holder.bus_seater = (TextView) convertView
					.findViewById(R.id.bus_seater);
			holder.currency = (TextView) convertView
					.findViewById(R.id.currency);
			holder.amount = (TextView) convertView.findViewById(R.id.amount);
			convertView.setTag(holder);
		} else {
			holder = (OrderHolder) convertView.getTag();
		}

		holder.txtDestination.setText(listArr.get(position).route_to);
		holder.txtSource.setText(listArr.get(position).route_from);
		String date = listArr.get(position).travelDepatureTime.toString()
				.replace("/Date(", "");
		date = date.replace(")/", "");

		long l = Long.parseLong(date);

		Date date_time = new Date(l);
		SimpleDateFormat df2 = new SimpleDateFormat("hh:mm:aa");
		String final_date = df2.format(date_time);

		holder.txtLeaveTime.setText(final_date);

		
		String date2 = listArr.get(position).travelArrivalTime.toString()
				.replace("/Date(", "");
		date2 = date2.replace(")/", "");
		long l2 = Long.parseLong(date2);

		Date date_time_2 = new Date(l2);
		SimpleDateFormat df1 = new SimpleDateFormat("hh:mm:aa");
		String final_date_2 = df1.format(date_time_2);

		holder.txtReachTime.setText(final_date_2);
		holder.txtTime.setText(listArr.get(position).bus_seats.toString());
		holder.bus_seater.setText(listArr.get(position).bus_type.toString());
		String Curr = "Rs:";
		holder.currency.setText(Curr);
		holder.amount.setText(listArr.get(position).price.toString());

	

		return convertView;

	}

	public static class OrderHolder {
		TextView txtSource;
		TextView txtDestination;
		TextView txtTime;
		TextView txtLeaveTime;
		TextView txtReachTime;
		TextView bus_seater;
		TextView currency;
		TextView amount;
	}
}
